package com.xiangyang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyang.annotations.OperationLog;
import com.xiangyang.entity.Department;
import com.xiangyang.entity.dto.Result;
import com.xiangyang.mapper.IDepartmentMapper;
import com.xiangyang.service.IDepartmentServer;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentImpl extends ServiceImpl<IDepartmentMapper, Department> implements IDepartmentServer {
   @Autowired
   private IDepartmentMapper departmentMapper;
    @Override
    @OperationLog(title = "添加科室", businessType = "新增")
    public Result addDepartment(Department department) {
        if(department.getDepartmentName()==null||department.getDescribes()==null){
            return Result.error("输入有误");
        }
        int insert = departmentMapper.insert(department);
        if(insert!=1){
            return Result.error("添加失败");
        }

        return Result.ok(department);
    }
@OperationLog(title = "删除科室", oldDataMethod = "getDepartmentList", businessType = "删除")
    @Override
    public Result deleteDepartment(Long departmentId) {
        if (departmentId==null){
            return Result.error("输入有误");
        }
        int i = departmentMapper.deleteById(departmentId);
        if(i<0){
            return Result.error("删除失败");
        }
        return Result.ok();
    }
    @OperationLog(title = "修改科室", businessType = "修改", oldDataMethod = "getDepartmentList",newDataMethod = "getDepartmentList")
    @Override
    public Result updateDepartment(Department department) {

        department.setDepartmentId(department.getDepartmentId());
        int i = departmentMapper.updateById(department);
         if(i<0){
             return Result.error("修改失败");
         }
         return Result.ok(i>0?true:false);
    }

    @Override
    public Result getDepartmentList(Department department) {
        Map<String,Object>map = new HashMap<>();
        map.put("department_name",department.getDepartmentName());
        map.put("describes",department.getDescribes());
        List<Department> departments = departmentMapper.selectByMap(map);

        return Result.ok(departments);
    }

    @Override
    public Result getDepartmentList() {
        return Result.ok(getBaseMapper().selectList(null));
    }
}
